package customerEnter.Frame.servlet;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import customerEnter.Frame.DaoDataTransmission.FrameBeanSuper;
import customerEnter.Frame.Utils.Log4j;
import customerEnter.Frame.Utils.WhetherToClass;
import customerEnter.tool.UserInfoCounts;

/**
 * 繼承此類后需要使用父類的doget方法
 * 
 * 类名称：HttpServletFram 类描述： 创建人：dy 创建时间：2016-6-8 上午11:54:58
 * 
 */
public abstract class HttpServletFram<T extends FrameBeanSuper> extends HttpServlet {

	public T					Parameter;

	private Class<T>			class1;

	/**
	 * serialVersionUID:TODO（用一句话描述这个变量表示什么）
	 * 
	 * @since Ver 1.1
	 */
	private static final long	serialVersionUID	= 8018375606373178660L;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		setRequestParameter(req);
		super.service(req, resp);
	}

	/**
	 * 初始化的時候
	 */
	@SuppressWarnings({ "unchecked" })
	public void setRequestParameter(HttpServletRequest request) {

		try {
			Parameter = (T) getBaseBens(class1).newInstance();
		} catch (Exception e) {
			System.out.println("獲取泛型失敗");
			return;
		}
		System.out.println(UserInfoCounts.getRequest().getParameterMap());
		Map<String, String[]> maps = request.getParameterMap();
		Enumeration<String> Enumer = request.getParameterNames();
		while (Enumer.hasMoreElements()) {
			String Mapkey = (String) Enumer.nextElement();
			String value = " ";
			for (int i = 0; i < maps.get(Mapkey).length; i++) {
				value += maps.get(Mapkey)[i];
			}
			try {
				Parameter.getClass().getField(Mapkey).set(Parameter, value);
			} catch (Exception e) {
				Log4j.Log4jOutInfo("私有的屬性：" + e.getMessage());
				setParameterObject(Parameter, Mapkey, value);
			}
		}
	}

	/**
	 * 設置值
	 */
	public void setParameterObject(T Parameter, String Mapkey, String value) {
		try {
			for (Field field : Parameter.getClass().getDeclaredFields()) {
				if (Mapkey.equals(field.getName())) {
					field.setAccessible(true);
					if (WhetherToClass.IfIntOrInteger(field.getType().getName())) {
						int val = Integer.parseInt(value.trim());
						field.set(Parameter, val);
					} else if (WhetherToClass.IfdobleOrDoble(field.getType().getName())) {
						field.set(Parameter, Double.parseDouble(value));
					} else if (WhetherToClass.IfFloatOrfloat(field.getType().getName())) {
						field.set(Parameter, Float.parseFloat(value));
					} else {
						field.set(Parameter, value);
					}
				}
			}
		} catch (Exception e) {
			System.out.println("自動封裝設置值失敗");
		}

	}

	private Class<?> getBaseBens(Class<?> classObject) {
		Class<?> ClassNew = null;
		if (classObject == null) {
			try {
				ClassNew = Class.forName(((Class<?>) ((ParameterizedType) getClass().getGenericSuperclass())
						.getActualTypeArguments()[0]).getName());
			} catch (ClassNotFoundException e) {
				System.out.println("獲取泛型失敗");
			}
		}
		return ClassNew;
	}
}
